package generator.mybatisplus;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.sql.Types;
import java.util.*;

/**
 * MybatisplusApplication.
 * 2021/3/13
 *
 * @author machuanpeng
 */
@SpringBootApplication
public class MybatisplusApplication {

    public static void main(String[] args) {
        plusGenerator();
        //SpringApplication.run(MybatisplusApplication.class, args);
    }

    private static void plusGenerator() {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("generator");

        // 数据源配置
        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(resourceBundle.getString("datasource.url"), resourceBundle.getString("datasource.user"), resourceBundle.getString("datasource.password"));
        dataSourceConfigBuilder.driverClassName(resourceBundle.getString("datasource.driver"));

        // 代码生成器
        FastAutoGenerator.create(dataSourceConfigBuilder)
                // 全局配置
                .globalConfig((scanner, builder) -> {
                    builder.outputDir(System.getProperty("user.dir") + "/mybatisplus/src/main/java");
                    builder.author(resourceBundle.getString("author.name"));
                    builder.enableSwagger();
                    builder.disableOpenDir();
                    builder.dateType(DateType.ONLY_DATE);
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode==Types.TINYINT || typeCode==Types.BIT || typeCode==Types.SMALLINT) {
                        //自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);
                }))
                // 包配置
                .packageConfig((scanner, builder) -> {
                    builder.parent(resourceBundle.getString("package.base"));
                    builder.entity(resourceBundle.getString("package.entity"));
                    builder.mapper(resourceBundle.getString("package.mapper"));
                    builder.service(resourceBundle.getString("package.service"));
                    builder.serviceImpl(resourceBundle.getString("package.service.impl"));
//                    builder.moduleName("");
                })
                // 策略配置
                .strategyConfig((scanner, builder) -> {
//                    builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔，所有输入 %")));
                    builder.addInclude(getTables(resourceBundle.getString("tables")));
                    //entity的策略配置
                    builder.entityBuilder()
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .idType(IdType.AUTO)
                            .enableLombok()
                            .versionColumnName("version")
                            .enableFileOverride()
                            .enableTableFieldAnnotation()
                            //.javaTemplate("/templates/entity_common.java.vm")
                            //.superClass("")
                            //.logicDeleteColumnName("is_delete")
                            //.formatFileName("%sEntity")
                            .build();
                    builder.mapperBuilder()
                            .enableFileOverride()
                            .enableBaseColumnList()
                            .build();
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .enableFileOverride()
                            //.serviceImplTemplate("/templates/serviceImpl_batch.java.vm")
                            .build();
                    builder.controllerBuilder()
                            .enableRestStyle()
                            .enableFileOverride()
                            //.enableHyphenStyle()
                            .build();
                })
                .injectionConfig((builder) -> {

                })
                .execute();
    }

    protected static List<String> getTables(String tables) {
        return "%".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
